<template>
  <postloan-file
    v-if="loanAfter"
    @close="afterClose"
    :visible.sync="afterVisible"
    :related-data="customerRelated"
  ></postloan-file>
  <el-dialog
    v-else
    :visible.sync="visible"
    :close-on-click-modal="false"
    :before-close="beforeClose"
    append-to-body
    top="30px"
    width="1000px"
  >
    <el-tabs>
      <el-tab-pane v-for="(p, index) of tabs" :key="index" :label="p.title">
        <component :is="p.component" v-bind="p.bind"></component>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
import CreditCard from '@/credit/components/creditFile/CreditCard'
import AttachView from '@/credit/components/AttachView'
import RecommendFile from '@/credit/views/city/program/RecommendFile'
import InvestigateReport from '@/credit/components/creditFile/InvestigateReport'
import ApplyFile from '@/credit/views/city/self/ApplyFile'
import ReviewFileView from '@/credit/views/county/mission/components/ReviewFileView'
import ReviewCreditView from '@/credit/views/county/mission/components/ReviewCreditView'
import ReviewFile from '@/credit/views/county/unionProvince/components/ReviewFile'

import LetterView from '@/credit/views/county/mission/components/LetterView'
import RegisterContractView from '@/credit/views/county/mission/RegisterContractView'
import RegisterContractViewGuard from '@/credit/views/county/mission/RegisterContractViewGuard'
import RegisterContractViewAddress from '@/credit/views/county/mission/RegisterContractViewAddress'
import RegisterLoanNotice from '@/credit/views/county/mission/RegisterLoanNotice'
import RegisterReply from '@/credit/views/county/mission/RegisterReply'
import PostloanFile from '@/credit/views/city/mission/PostloanFile'

export default {
  components: {
    CreditCard,
    AttachView,
    RecommendFile,
    InvestigateReport,
    ApplyFile,
    ReviewFileView,
    ReviewCreditView,
    ReviewFile,
    LetterView,
    RegisterContractView,
    RegisterContractViewGuard,
    RegisterContractViewAddress,
    RegisterLoanNotice,
    RegisterReply,
    PostloanFile,
  },
  props: {
    visible: Boolean,
    customerRelated: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      tabs: [],
      afterVisible: false,
      contractLoading: false,
    }
  },

  computed: {
    queryId() {
      return this.customerRelated.customer_related_id
    },
    isLimit() {
      return +this.customerRelated.gua_type === 3
    },
    isSelf() {
      return +this.customerRelated.gua_type === 2
    },
    isCityJoint() {
      return +this.customerRelated.gua_type === 1
    },
    isProvinceJoint() {
      return +this.customerRelated.gua_type === 4
    },
    isHistory() {
      return +this.customerRelated.is_history === 1
    },
    // 政府推荐
    isGovRecommend() {
      return +this.customerRelated.submit_type === 1
    },
    // 所有流程已拒绝
    isConfuse() {
      return +this.customerRelated.confuse_status === 3
    },
    // 已立项
    itemSuccess() {
      return +this.customerRelated.item_status === 1
    },
    // 直担审批 或者 联担初审
    countySuccess() {
      return +this.customerRelated.project_res_status === 2
    },
    // 联担复审
    citySuccess() {
      return +this.customerRelated.city_project_res_status === 2
    },
    // 电子保函已发
    sendLetter() {
      return +this.customerRelated.ele_letter_status > 0
    },
    // 联保函已发
    sendGuaLetter() {
      return +this.customerRelated.gua_letter_status > 0
    },
    // 已设置委托合同
    ensureSet() {
      return +this.customerRelated.ensure_status > 0
    },
    // 已设置反担保合同
    guaSet() {
      return +this.customerRelated.gua_status > 0
    },
    // 放款通知书已发
    sendLoanNotice() {
      return +this.customerRelated.loan_notice_status > 0
    },
    // 贷后项目
    loanAfter() {
      return +this.customerRelated.loan_notice_status === 2
    },

    creditCard() {
      return {
        component: 'credit-card',
        title: '档案卡',
        bind: { queryId: this.queryId, viewMode: true },
      }
    },
    investigateReport() {
      return {
        component: 'investigate-report',
        title: '调查报告',
        bind: { queryId: this.queryId, viewMode: true },
      }
    },
    recommendFile() {
      return {
        component: 'recommend-file',
        title: '政府推荐函',
        bind: { relatedData: this.customerRelated },
      }
    },
    attachView() {
      return {
        component: 'attach-view',
        title: '附件',
        bind: { queryId: this.queryId, visible: this.visible },
      }
    },
    applyFile() {
      return {
        component: 'apply-file',
        title: '项目申请书',
        bind: { relatedData: this.customerRelated },
      }
    },
    // 区县/直担
    reviewFileView() {
      return {
        component: 'review-file-view',
        title: '审批表',
        bind: { relatedData: this.customerRelated },
      }
    },
    // 市联担业务
    reviewCreditView() {
      return {
        component: 'review-credit-view',
        title: '联担审批表',
        bind: { relatedData: this.customerRelated },
      }
    },
    // 省联担业务
    reviewFile() {
      return {
        component: 'review-file',
        title: '审批表',
        bind: { relatedData: this.customerRelated, viewMode: true },
      }
    },
    letterView() {
      return {
        component: 'letter-view',
        title: '担保意向函',
        bind: { relatedData: this.customerRelated },
      }
    },
    registerContractView() {
      return {
        component: 'register-contract-view',
        title: '委托担保合同',
        bind: { relatedData: this.customerRelated, loading: this.contractLoading },
      }
    },
    registerContractViewGuard() {
      return {
        component: 'register-contract-view-guard',
        title: '反担保合同',
        bind: { relatedData: this.customerRelated, loading: this.contractLoading },
      }
    },
    registerContractViewAddress() {
      return {
        component: 'register-contract-view-address',
        title: '地址确认书',
        bind: { relatedData: this.customerRelated, loading: this.contractLoading },
      }
    },
    registerLoanNotice() {
      return {
        component: 'register-loan-notice',
        title: '放款通知书',
        bind: { relatedData: this.customerRelated, viewMode: true },
      }
    },
    registerReply() {
      return {
        component: 'register-reply',
        title: '放款登记',
        bind: { relatedData: this.customerRelated, viewMode: true },
      }
    },
    // 放款登记模块，直担联担显示一致
    approvalSuccess() {
      return (
        (this.isSelf && this.countySuccess) ||
        ((this.isCityJoint || this.isProvinceJoint) && this.citySuccess)
      )
    },
  },

  watch: {
    visible() {
      this.afterVisible = this.visible
    },
    customerRelated: {
      handler: function () {
        this.setTabs()
        if (this.ensureSet || this.guaSet) {
          this.getContract(this.queryId)
        }
      },
      deep: true,
      immediate: true,
    },
  },

  created() {},
  methods: {
    setTabs() {
      this.tabs = []
      if (this.isLimit) {
        return this.tabs.push(this.creditCard)
      }
      if (this.isHistory) {
        this.tabs.push(this.creditCard)
        if (this.isGovRecommend) {
          this.tabs.push(this.recommendFile)
        }
        if (this.sendLoanNotice) {
          this.tabs.push(this.registerReply)
        }
        this.tabs.push(this.attachView)
        return
      }
      if (this.isConfuse) {
        this.tabs.push(this.creditCard)
        if (this.isGovRecommend) {
          this.tabs.push(this.recommendFile)
        }
        this.tabs.push(this.attachView)
        return
      }

      if (this.itemSuccess) {
        this.tabs.push(this.investigateReport)
      } else {
        this.tabs.push(this.creditCard)
      }

      if (this.isGovRecommend) {
        this.tabs.push(this.recommendFile)
      }

      if (this.approvalSuccess) {
        if (this.sendLetter) {
          this.tabs.push(this.letterView)
        }
        if (this.ensureSet) {
          this.tabs.push(this.registerContractView)
        }
        if (this.guaSet) {
          this.tabs.push(this.registerContractViewGuard)
          this.tabs.push(this.registerContractViewAddress)
        }
        if (this.sendLoanNotice) {
          this.tabs.push(this.registerLoanNotice)
        } else {
          if (this.isProvinceJoint) {
            this.tabs.push(this.reviewFile)
          } else if (this.isCityJoint) {
            this.tabs.push(this.reviewCreditView)
          } else {
            this.tabs.push(this.reviewFileView)
          }
        }
        this.tabs.push(this.attachView)
        return
      }

      if (this.isSelf) {
        if (this.itemSuccess) {
          this.tabs.push(this.applyFile)
          this.tabs.push(this.reviewFileView)
          if (this.sendLetter) {
            this.tabs.push(this.letterView)
          }
        }
        this.tabs.push(this.attachView)
        return
      }
      if (this.isCityJoint || this.isProvinceJoint) {
        if (this.sendLetter) {
          this.tabs.push(this.letterView)
        }
        if (this.countySuccess) {
          if (this.isProvinceJoint) {
            this.tabs.push(this.reviewFile)
          } else {
            this.tabs.push(this.reviewFileView)
          }
        } else if (this.itemSuccess) {
          this.tabs.push(this.applyFile)
        }
        this.tabs.push(this.attachView)
      }
    },
    async getContract(relatedId) {
      if (!relatedId) return
      this.contractLoading = true
      await this.$utils.promise(
        Promise.all([
          this.$store.dispatch('getEntrustContract', relatedId),
          this.$store.dispatch('getGuardsData', relatedId),
        ])
      )
      this.contractLoading = false
    },
    beforeClose() {
      this.$emit('update:visible', false)
    },
    afterClose() {
      this.afterVisible = false
      this.$emit('update:visible', false)
    },
  },
}
</script>
<style lang="scss" scoped></style>
